package com.example.hackover_client;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.location.*;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TableRow;
import android.widget.TextView;

public class Runner implements LocationListener {
	
	private Main mainApp;
	private Location cLocation;
	
	private EditText txtStatus;
	private TextView txtRez;
	
	public Runner(Main app){
		mainApp=app;
		
		txtStatus=(EditText) mainApp.findViewById(R.id.tstStatus);
		txtRez=(TextView) mainApp.findViewById(R.id.txtRez);
	}

	@Override
	public void onLocationChanged(Location location) {
		cLocation=location;
		
		//broadcast status
		Thread broadcastThread = new Thread() {
            @Override
            public void run() {
              try {
                 SoapObject request = new SoapObject(mainApp.NAMESPACE, "Ping");    
                 request.addProperty("uGUID", mainApp.usr_GUID);
                 request.addProperty("msg", txtStatus.getText().toString());
                 request.addProperty("lng",Double.toString(cLocation.getLongitude()));
                 request.addProperty("lat",Double.toString(cLocation.getLatitude()));
                 
                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                 envelope.dotNet=true;
                 envelope.setOutputSoapObject(request);
                  
                 HttpTransportSE ht = new HttpTransportSE(mainApp.URL);
                 ht.call("http://tempuri.org/Ping", envelope);
                 final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                 final String strResp = response.toString();
         
                 mainApp.runOnUiThread (new Runnable(){ 
             public void run() {
            	 
                   }
               });
              } 
             catch (Exception e) {
                 e.printStackTrace();
             }
            }
          };
          broadcastThread.start();
          
          //retreive list
        //broadcast status
  		Thread updateThread = new Thread() {
              @Override
              public void run() {
                try {
                   SoapObject request = new SoapObject(mainApp.NAMESPACE, "GetList");    
                   request.addProperty("uGUID", mainApp.usr_GUID);
                   request.addProperty("lng",Double.toString(cLocation.getLongitude()));
                   request.addProperty("lat",Double.toString(cLocation.getLatitude()));
                   
                   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                   envelope.dotNet=true;
                   envelope.setOutputSoapObject(request);
                    
                   HttpTransportSE ht = new HttpTransportSE(mainApp.URL);
                   ht.call("http://tempuri.org/GetList", envelope);
                   final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                   final String strResp = response.toString();
           
                   mainApp.runOnUiThread (new Runnable(){ 
               public void run() {
            	   txtRez.setText(strResp.replace("|", "\n"));
                     }
                 });
                } 
               catch (Exception e) {
                   e.printStackTrace();
               }
              }
            };
            updateThread.start();
		
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
